import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wan_android/utils/platform_utils.dart';

///
/// 添加常用Header
///
class HeaderInterceptor extends InterceptorsWrapper {
  @override
  onRequest(RequestOptions options) async {
    var appVersion = await PlatformUtils.getAppVersion();
    options.headers['app_version'] = appVersion;
    options.headers['platform'] = Platform.operatingSystem;
    return super.onRequest(options);
  }
}

class LoggerInterceptor extends InterceptorsWrapper {
  @override
  Future onRequest(RequestOptions options) {
    debugPrint('**********http onRequest**********\n'
        'request url--->${options.baseUrl}${options.path}\n'
        'queryParameters---> ${options.queryParameters}\n' +
        '********************\n');
    return super.onRequest(options);
  }

  @override
  Future onResponse(Response response) {
    debugPrint('**********http onResponse**********\n'
        'statusCode--->${response.statusCode}\n'
        'data---> ${response.data?.toString()}\n' +
        '********************\n');
    return super.onResponse(response);
  }

  @override
  Future onError(DioError err) {
    debugPrint('**********http onError**********\n'
        'message--->${err.message}\n'
        'baseUrl--->${err.request.baseUrl}\n'
        'path---> ${err.request.path}\n' +
        '********************\n');
    return super.onError(err);
  }
}
